package com.hongyuan.sellfitness.ui.person.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.ViewModelProviders;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.AppViewModelFactory;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomActivity;
import com.hongyuan.sellfitness.databinding.ActivityCancelReservationBinding;
import com.hongyuan.sellfitness.my_view.scllor_view.UnitBeanUtils;
import com.hongyuan.sellfitness.ui.person.beans.CancelReservationBean;
import com.hongyuan.sellfitness.ui.person.view_mode.CancelReservationViewModel;
import com.hongyuan.sellfitness.utils.CustomDialog;

import java.util.List;

import me.tatarka.bindingcollectionadapter2.BR;

public class CancelReservationActivity extends CustomActivity<ActivityCancelReservationBinding, CancelReservationViewModel> {

    //配送方式选择工具类
    private UnitBeanUtils rUtils;
    private List<CancelReservationBean.ListBean> mList;

    //当前选取的缘由id
    private String cancel_reason_id = "";

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_cancel_reservation;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public CancelReservationViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(CancelReservationViewModel.class);
    }

    @Override
    public void initView() {
        super.initView();
        setOnRefresh(binding.refresh,REFRESH_0X4);

        binding.comBack.setOnClickListener(view -> finish());

        rUtils = new UnitBeanUtils<CancelReservationBean.ListBean>() {
            @Override
            public String unit(CancelReservationBean.ListBean o) {
                return String.valueOf(o.getReason_id());
            }

            @Override
            public String unit_cn(CancelReservationBean.ListBean o) {
                return o.getName();
            }
        };

        binding.selectBox.setOnClickListener(v -> {
            CustomDialog.scroller(this, rUtils.getUnitList(mList), "选择取消原因", (v1, message,id) -> {
                binding.reasonName.setText(message);
                cancel_reason_id = rUtils.getUseId(message);
            });
        });

        binding.cancel.setOnClickListener(view -> {

            CustomDialog.promptDialog(this, "确定要取消预约？", "确定!", "不确定?", false, v -> {
                if(v.getId() == R.id.isOk){
                    //取消预约
                    viewModel.clearParams().setParams("cpa_id",getBundle().getString("cpa_id"))
                            .setParams("cancel_reason_id",cancel_reason_id);

                    if(TextUtils.isEmpty(binding.notes.getText().toString())){
                        viewModel.setParams("cancel_note",binding.reasonName.getText().toString());
                    }else{
                        viewModel.setParams("cancel_note",binding.notes.getText().toString());
                    }

                    viewModel.requestData(Constants.CANCEL_COURSE_PRIVITE_APPOINTMENT_NEW);
                }
            });

        });

        binding.callTel.setOnClickListener(view -> callTel(getBundle().getString("tel")));
    }

    @Override
    public void initData() {
        super.initData();

        viewModel.requestData(Constants.GET_CANCEL_RESON);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.showReason.observe(this, listBeans -> {
            mList = listBeans;
            cancel_reason_id = String.valueOf(listBeans.get(0).getReason_id());
            binding.reasonName.setText(listBeans.get(0).getName());
        });
    }
}
